package labox.innovation.gameserver.network.clientpackets;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import labox.innovation.config.Config;
import labox.innovation.gameserver.LoginServerThread;
import labox.innovation.gameserver.ThreadPoolManager;
import labox.innovation.gameserver.network.serverpackets.CharDeleteResult;
import labox.innovation.gameserver.network.serverpackets.CharSelectionInfo;

/**
 * 客户端请求删除角色
 * 
 * @version $Revision: 1.8.2.1.2.3 $ $Date: 2005/03/27 15:29:30 $
 */
public final class CharacterDelete extends L2GameClientPacket {

	private static final String _C__0C_CHARACTERDELETE = "[C] 0C CharacterDelete";

	private static Logger _log = LoggerFactory.getLogger(CharacterDelete.class.getName());

	// cd
	private int _charSlot;

	@Override
	protected void readImpl() {
		_charSlot = readByte();
	}

	@Override
	protected void runImpl() {

		if (_log.isDebugEnabled())
			_log.debug("deleting slot:{}", _charSlot);

		ThreadPoolManager.getInstance().execute(new Runnable() {

			@Override
			public void run() {
				boolean isDeleted = false;
				try {

					byte answer = getClient().markToDeleteChar(_charSlot);

					switch (answer) {
					default:
					case -1: // Error
						break;
					case 0: // Success!
						sendPacket(new CharDeleteResult(CharDeleteResult.REASON_DELETION_SUCCESS));
						isDeleted = true;
						break;
					case 1:
						sendPacket(new CharDeleteResult(CharDeleteResult.REASON_YOU_MAY_NOT_DELETE_CLAN_MEMBER));
						break;
					case 2:
						sendPacket(new CharDeleteResult(CharDeleteResult.REASON_CLAN_LEADERS_MAY_NOT_BE_DELETED));
						break;
					}
				} catch (Exception e) {
					_log.error("CharacterDelete Error account:" + getClient().getAccountName() + " delete slot:" + _charSlot, Config.SERVER_ID, e);
				}

				CharSelectionInfo cl = new CharSelectionInfo(getClient().getAccountName(), getClient().getSessionId(), 0);
				sendPacket(cl);
				getClient().setCharSelection(cl.getCharInfo());
			}

		});

	}

	/* (non-Javadoc)
	 * @see labox.innovation.gameserver.clientpackets.ClientBasePacket#getType()
	 */
	@Override
	public String getType() {
		return _C__0C_CHARACTERDELETE;
	}
}
